package com.example.ivideo_2211a.fabu

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.fragment.app.viewModels
import com.alibaba.android.arouter.launcher.ARouter
import com.example.ivideo_2211a.R

class CreateFragment : Fragment() {
    private val viewModel by viewModels<CreateViewModel>()

    @SuppressLint("MissingInflatedId")
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_create, container, false)
        val uploadBtn = view.findViewById<View>(R.id.ll_upload)
        uploadBtn.setOnClickListener {
            // 跳转到发布视频页面
            requireActivity().supportFragmentManager.beginTransaction()
                .replace(R.id.fragmentContainerView, PublishVideoFragment())
                .addToBackStack(null)
                .commit()
        }
        // 监听发布结果（StateFlow收集）
        lifecycleScope.launchWhenStarted {
            viewModel.viewState.collect { state ->
                when (state) {
                    is CreateViewState.Success -> Toast.makeText(requireContext(), state.msg, Toast.LENGTH_SHORT).show()
                    is CreateViewState.Error -> Toast.makeText(requireContext(), state.error, Toast.LENGTH_SHORT).show()
                    else -> {}
                }
            }
        }
        val take = view.findViewById<LinearLayout>(R.id.take_phone)
        take.setOnClickListener {
            ARouter.getInstance().build("/take/TakeActivity").navigation()
        }
        return view
    }
}
